﻿using PickingGuide.Business;
using PickingGuide.Model;
using ScanConfirmForm;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PickingGuideForm
{
    public partial class FrmManualInput : Form
    {
        List<ManualSetupModel> Queues = new List<ManualSetupModel>();
        public Action<List<ManualSetupModel>> RefreshUiEvent;
        PickingSetupBLL pickingSetupBLL = new PickingSetupBLL();
        RackSetupBLL rackSetupBLL = new RackSetupBLL();
        ManualSetupBLL manualSetupBLL = new ManualSetupBLL();
        public FrmManualInput(List<ManualSetupModel> queues)
        {
            InitializeComponent();
            Queues = queues;
            //全屏，覆盖任务栏
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;
            }

            //dgvRack.ClearSelection();
            dgvRack.AutoGenerateColumns = false;
            dgvRack.ReadOnly = true;
            dgvRack.AddColumn("手动拣货名称", "Name");
            dgvRack.AddColumn("部品1", "Picking1ID");
            dgvRack.AddColumn("部品2", "Picking2ID");

            dgvSet.AutoGenerateColumns = false;
            dgvSet.ReadOnly = true;
            dgvSet.AddColumn("名称", "Name");
            dgvSet.AddColumn("部品1", "Picking1ID");
            dgvSet.AddColumn("部品2", "Picking2ID");
        }

        private void FrmManualInput_Load(object sender, EventArgs e)
        {
            Search_Rack(Queues);
            Search_PickingSet();
        }

        /// <summary>
        /// 查询--拣货队列
        /// </summary>
        private void Search_Rack(List<ManualSetupModel> queues)
        {
            Queues = queues;
            if (queues == null || queues.Count <= 0)
                return;
            this.Invoke(new MethodInvoker(() =>
            {
                dgvRack.DataSource = null;
                dgvRack.DataSource = queues;
            }));
        }

        /// <summary>
        /// 查询--拣货配置
        /// </summary>
        private void Search_PickingSet()
        {
            var list = manualSetupBLL.GetModelAll();
            if (list == null || list.Count <= 0)
                return;
            this.Invoke(new MethodInvoker(() =>
            {
                dgvSet.DataSource = null;
                dgvSet.DataSource = list;
            }));
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            Search_Rack(Queues);
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            FrmManualAdd frm = new FrmManualAdd(Queues);
            frm.RefreshUiEvent += Search_Rack;
            frm.Show();
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            var model = dgvRack.CurrentRow.DataBoundItem as ManualSetupModel;
            if (model == null)
            {
                MessageBox.Show("请先选择要修改的货架！");
                return;
            }
            else
            {
                FrmManualAdd frm = new FrmManualAdd(Queues,model);
                frm.RefreshUiEvent += Search_Rack;
                frm.Show();
            }
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {

        }

        private void dgvRack_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                e.FormattingApplied = true;
                DataGridViewRow row = dgvRack.Rows[e.RowIndex];
                if (row != null)
                {
                    var id = long.Parse(row.Cells[e.ColumnIndex].Value.ToString());
                    var pickingSetup = pickingSetupBLL.GetModel(id);
                    if (pickingSetup != null)
                    {
                        e.Value = string.Format("{0}",
                        pickingSetup.Name);
                    }
                    else
                    {
                        e.Value = string.Format("{0}",
                        "未配置");
                    }
                }
            }
            else if (e.ColumnIndex == 2)
            {
                e.FormattingApplied = true;
                DataGridViewRow row = dgvRack.Rows[e.RowIndex];
                if (row != null)
                {
                    var id = long.Parse(row.Cells[e.ColumnIndex].Value.ToString());
                    var pickingSetup = pickingSetupBLL.GetModel(id);
                    if (pickingSetup != null)
                    {
                        e.Value = string.Format("{0}",
                        pickingSetup.Name);
                    }
                    else
                    {
                        e.Value = string.Format("{0}",
                        "未配置");
                    }
                }
            }
        }

        private void btnSet_Search_Click(object sender, EventArgs e)
        {

        }

        private void btnSet_Add_Click(object sender, EventArgs e)
        {
            FrmManualSetup frm = new FrmManualSetup();
            frm.RefreshUiEvent += Search_PickingSet;
            frm.Show();
        }

        private void btnSet_Update_Click(object sender, EventArgs e)
        {
            var model = dgvSet.CurrentRow.DataBoundItem as ManualSetupModel;
            if (model == null)
            {
                MessageBox.Show("请先选择要修改的手动拣货配置！");
                return;
            }
            else
            {
                FrmManualSetup frm = new FrmManualSetup(model);
                frm.RefreshUiEvent += Search_PickingSet;
                frm.Show();
            }
        }

        private void btnSet_Del_Click(object sender, EventArgs e)
        {
            var model = dgvSet.CurrentRow.DataBoundItem as ManualSetupModel;
            if (model == null)
            {
                MessageBox.Show("请先选择要删除的手动拣货配置！");
                return;
            }
            else
            {
                MessageBoxButtons mess = MessageBoxButtons.OKCancel;
                DialogResult dr = MessageBox.Show("是否删除该拣货明细？", "提示", mess);
                if (dr == DialogResult.OK)
                {
                    manualSetupBLL.Delete(model);
                    Search_PickingSet();
                }
            }
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            RefreshUiEvent?.BeginInvoke(Queues, null, null);
            this.Close();
        }

        private void btnClose1_Click(object sender, EventArgs e)
        {
            RefreshUiEvent?.BeginInvoke(Queues, null, null);
            this.Close();
        }

        private void dgvSet_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                e.FormattingApplied = true;
                DataGridViewRow row = dgvSet.Rows[e.RowIndex];
                if (row != null)
                {
                    var id = long.Parse(row.Cells[e.ColumnIndex].Value.ToString());
                    var pickingSetup = pickingSetupBLL.GetModel(id);
                    if (pickingSetup != null)
                    {
                        e.Value = string.Format("{0}",
                        pickingSetup.Name);
                    }
                    else
                    {
                        e.Value = string.Format("{0}",
                        "未配置");
                    }
                }
            }
            else if (e.ColumnIndex == 2)
            {
                e.FormattingApplied = true;
                DataGridViewRow row = dgvSet.Rows[e.RowIndex];
                if (row != null)
                {
                    var id = long.Parse(row.Cells[e.ColumnIndex].Value.ToString());
                    var pickingSetup = pickingSetupBLL.GetModel(id);
                    if(pickingSetup != null)
                    {
                        e.Value = string.Format("{0}",
                        pickingSetup.Name);
                    }else
                    {
                        e.Value = string.Format("{0}",
                        "未配置");
                    }
                }
            }
        }
    }
}
